package com.sumainfor.pc.market.service;


import com.qiniu.util.Json;
import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.HtmlUtil;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.TcmktownerMapper;
import com.sumainfor.pc.market.dao.UtilsMapper;
import com.sumainfor.pc.market.feign.ZuulFeign.ZuulFeign;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 租客端租客成为业主
 */

@Service
public class TcmktownerService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(TcmktownerService.class);

    @Autowired
    TcmktownerMapper tcmktownerMapper;

    @Autowired
    UtilsMapper utilsMapper;

    @Autowired
    ZuulFeign ZuulFeign;

    public JsonResult AddCommunity(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            Map<String, Object> uuid = utilsMapper.getUUID(new HashMap<String, Object>() {{
                put("uuid", "OwId");
                put("database", Constants.DATA_SOURCE_TCMKT);
                put("surface", "tcmkt_owner");
            }});
            if (uuid != null) {
                int number = Integer.valueOf(uuid.get("uuid").toString());
                params.put("OwId", ComUtils.getLocalTrmSeqNum("pt", number));//业主编号
            } else {
                params.put("OwId", ComUtils.getLocalTrmSeqNum("pt", 0));//业主编号
            }
            params.put("SoId", tcmktownerMapper.gettcmkt_source());//来源编号
            params.put("IfWechat", 1);
            params.put("term", "1");// 创建端 Insert端： 1 用户端 2 PC端 3 待定
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);


            Map<String,Object>message=new HashMap<String,Object>();
            message.put("MeType","1");//市场部经理
            message.put("Title","有新的业主待联系");
            message.put("Content", HtmlUtil.stringToHTML("您收到一条新的待联系业主，请及时处理！；",null,null));
//            message.put("by", ContextUtils.getUserId());//操作人
//            message.put("term","2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            //新增消息
            ZuulFeign.AddMessage(message);


            if (tcmktownerMapper.savetcmkt_owner(params) > 0) {
                result.putSuccess("操作成功");
            } else {
                result.putFailed("操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("E------->" + e);
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;
    }


    /**
     * 查询收藏的房源的状态
     *
     * @param params
     * @return
     */
    public JsonResult getHousingInfoState(List<Map<String, Object>> params) {
        JsonResult result = new JsonResult();
        List<Map<String, Object>> paralist = new ArrayList<Map<String, Object>>();
        if (params.size() != 0) {
            for (Map<String, Object> param : params) {
                Map<String, Object> resMap = new HashMap<String, Object>();
                resMap.putAll(param);
                Map<String, Object> m = tcmktownerMapper.getHousingInfoState(param);
                log.info("值:" + m);
                resMap.put("State", MapUtils.getString(m, "State", ""));
                paralist.add(resMap);
            }
            result.put(paralist);
        } else {
            result.put(new ArrayList<Map<String, Object>>());
        }
        return result;

    }

}
